#include "cv.h"

#include "utils.hh"


std::string get_basename (const char * name)
{

    std::string bn = name;

    size_t pos = bn.find_last_of("/");
    if(pos != std::string::npos)
	bn = bn.substr(pos+1, bn.size());
    
    pos = bn.find_last_of(".");
    if(pos != std::string::npos)
	bn.replace(pos, 1, "_");//remove the dot    }	
    
    return bn;
}


double calc_prob(double val, const CvMat *pdf_samples)
{


    double m, s;
    CvScalar mean, stdv;

    if(pdf_samples->rows == 0)
	return 1.;

    cvAvgSdv(pdf_samples, &mean, &stdv);
    m = mean.val[0];
    s = stdv.val[0];
    
    if(s == 0)
	return 1.;
    
    return (1/sqrt(2*M_PI)/s) *exp( -0.5 * pow((m-val)/s, 2) );

}

double calc_prob(double val, std::vector<double>pdf_samples)
{


    CvMat *mat = cvCreateMat(pdf_samples.size(), 1, CV_64FC1);

    for(uint ai=0; mat->rows; ai++)
	cvmSet(mat, ai, 0, pdf_samples.at(ai));


    double v = calc_prob(val, mat);
    cvReleaseMat(&mat);


    return v;
}



int strtolower(char *dest, const char * ori)
{
    int i;
    i=0;
    while (ori[i] != '\0')
    {
	dest[i] = (char) tolower((int)ori[i]);
	i++;
    }
    dest[i] = '\0';
    return i;
}

